<template>
  <div class="supplier-container">
    <div class="supplier-header">
      <div class="header-left">
        <h2>供应商管理</h2>
        <div class="statistics">
          <span class="stat-item">总数: {{ statistics.totalCount || 0 }}</span>
          <span class="stat-item">启用: {{ statistics.activeCount || 0 }}</span>
          <span class="stat-item">禁用: {{ statistics.inactiveCount || 0 }}</span>
        </div>
      </div>
      <div class="header-right">
        <button @click="refreshData" class="refresh-btn">刷新</button>
        <button @click="addSupplier" class="add-btn">新增供应商</button>
      </div>
    </div>

    <!-- 搜索表单 -->
    <div class="search-form">
      <div class="search-row">
        <div class="search-item">
          <label>供应商编码：</label>
          <input v-model="supplierSearch.supplierCode" placeholder="请输入供应商编码" />
        </div>
        <div class="search-item">
          <label>供应商名称：</label>
          <input v-model="supplierSearch.supplierName" placeholder="请输入供应商名称" />
        </div>
        <div class="search-item">
          <label>联系人：</label>
          <input v-model="supplierSearch.contactPerson" placeholder="请输入联系人" />
        </div>
      </div>
      <div class="search-row">
        <div class="search-item">
          <label>联系电话：</label>
          <input v-model="supplierSearch.contactPhone" placeholder="请输入联系电话" />
        </div>
        <div class="search-item">
          <label>供应商类型：</label>
          <select v-model="supplierSearch.supplierType">
            <option value="">全部</option>
            <option value="企业">企业</option>
            <option value="个人">个人</option>
            <option value="政府">政府</option>
          </select>
        </div>
        <div class="search-item">
          <label>所属行业：</label>
          <input v-model="supplierSearch.industry" placeholder="请输入所属行业" />
        </div>
      </div>
      <div class="search-row">
        <div class="search-item">
          <label>助记码：</label>
          <input v-model="supplierSearch.mnemonicCode" placeholder="请输入助记码" />
        </div>
        <div class="search-item">
          <label>状态：</label>
          <select v-model="supplierSearch.isActive">
            <option value="">全部</option>
            <option value="true">启用</option>
            <option value="false">禁用</option>
          </select>
        </div>
        <div class="search-item"></div>
      </div>
      <div class="search-actions">
        <button @click="searchSuppliers" class="search-btn">查询</button>
        <button @click="resetSearch" class="reset-btn">重置</button>
      </div>
    </div>

    <!-- 数据表格 -->
    <div class="table-container">
      <table class="data-table">
        <thead>
          <tr>
            <th>供应商编码</th>
            <th>供应商名称</th>
            <th>联系人</th>
            <th>联系电话</th>
            <th>地址</th>
            <th>所属行业</th>
            <th>供应商类型</th>
            <th>状态</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="supplier in suppliers" :key="supplier.id">
            <td>{{ supplier.supplierCode }}</td>
            <td>{{ supplier.supplierName }}</td>
            <td>{{ supplier.contactPerson }}</td>
            <td>{{ supplier.contactPhone }}</td>
            <td>
              <div class="address-cell">
                <span class="address-text">{{ supplier.address || '未设置' }}</span>
                <span v-if="supplier.longitude && supplier.latitude" class="coordinate-badge">
                  📍
                </span>
              </div>
            </td>
            <td>{{ supplier.industry }}</td>
            <td>{{ supplier.supplierType }}</td>
            <td>
              <span :class="supplier.isActive ? 'status-active' : 'status-inactive'">
                {{ supplier.isActive ? '启用' : '禁用' }}
              </span>
            </td>
            <td>
              <button @click="viewSupplier(supplier)" class="action-btn view-btn">查看</button>
              <button @click="editSupplier(supplier)" class="action-btn edit-btn">编辑</button>
              <button @click="deleteSupplier(supplier)" class="action-btn delete-btn">删除</button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <!-- 分页 -->
    <div class="pagination">
      <div class="pagination-info">
        共 {{ pagination.total }} 条记录，当前第 {{ pagination.currentPage }} 页
      </div>
      <div class="pagination-controls">
        <button 
          @click="changePage(pagination.currentPage - 1)" 
          :disabled="pagination.currentPage <= 1"
          class="page-btn"
        >
          上一页
        </button>
        <span class="page-info">{{ pagination.currentPage }} / {{ pagination.totalPages }}</span>
        <button 
          @click="changePage(pagination.currentPage + 1)" 
          :disabled="pagination.currentPage >= pagination.totalPages"
          class="page-btn"
        >
          下一页
        </button>
        <select v-model="pagination.pageSize" @change="handlePageSizeChange" class="page-size-select">
          <option value="10">10条/页</option>
          <option value="20">20条/页</option>
          <option value="50">50条/页</option>
        </select>
      </div>
    </div>

    <!-- 供应商表单 -->
    <SupplierForm
      v-if="showForm"
      :supplier="currentSupplier"
      :visible="showForm"
      @update:visible="showForm = $event"
      @success="handleFormSuccess"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { api } from '../utis/request'
import SupplierForm from '../components/SupplierForm.vue'

// 供应商管理相关
const suppliers = ref<any[]>([])
const supplierSearch = ref({
  supplierCode: '',
  supplierName: '',
  contactPerson: '',
  contactPhone: '',
  supplierType: '',
  isActive: '',
  industry: '',
  mnemonicCode: ''
})
const pagination = ref({
  currentPage: 1,
  pageSize: 10,
  total: 0,
  totalPages: 0
})

// 表单相关
const showForm = ref(false)
const currentSupplier = ref<any>(null)

// 统计数据
const statistics = ref({
  totalCount: 0,
  activeCount: 0,
  inactiveCount: 0
})

// 搜索供应商
const searchSuppliers = async () => {
  try {
    const params = {
      pageIndex: pagination.value.currentPage,
      pageSize: pagination.value.pageSize,
      supplierCode: supplierSearch.value.supplierCode,
      supplierName: supplierSearch.value.supplierName,
      contactPerson: supplierSearch.value.contactPerson,
      contactPhone: supplierSearch.value.contactPhone,
      supplierType: supplierSearch.value.supplierType,
      isActive: supplierSearch.value.isActive === 'true' ? true : supplierSearch.value.isActive === 'false' ? false : undefined,
      industry: supplierSearch.value.industry,
      mnemonicCode: supplierSearch.value.mnemonicCode
    }
    const response = await api.supplier.getList(params)
    suppliers.value = response.data?.data?.data || []
    pagination.value.total = response.data?.data?.totalCount || 0
    pagination.value.totalPages = Math.ceil(pagination.value.total / pagination.value.pageSize)
  } catch (error) {
    console.error('搜索供应商失败:', error)
  }
}

// 重置搜索
const resetSearch = () => {
  supplierSearch.value = {
    supplierCode: '',
    supplierName: '',
    contactPerson: '',
    contactPhone: '',
    supplierType: '',
    isActive: '',
    industry: '',
    mnemonicCode: ''
  }
  pagination.value.currentPage = 1
  searchSuppliers()
}

// 切换页码
const changePage = (page: number) => {
  if (page >= 1 && page <= pagination.value.totalPages) {
    pagination.value.currentPage = page
    searchSuppliers()
  }
}

// 切换每页条数
const handlePageSizeChange = () => {
  pagination.value.currentPage = 1
  searchSuppliers()
}

// 新增供应商
const addSupplier = () => {
  currentSupplier.value = null
  showForm.value = true
}

// 查看供应商
const viewSupplier = (supplier: any) => {
  currentSupplier.value = supplier
  showForm.value = true
}

// 编辑供应商
const editSupplier = (supplier: any) => {
  currentSupplier.value = supplier
  showForm.value = true
}

// 删除供应商
const deleteSupplier = (supplier: any) => {
  if (confirm(`确定要删除供应商 "${supplier.supplierName}" 吗？`)) {
    alert('删除功能开发中...')
  }
}

// 获取统计数据
const getStatistics = async () => {
  try {
    const response = await api.supplier.getStatistics()
    if (response.data?.code === 0) {
      statistics.value = response.data.data
    }
  } catch (error) {
    console.error('获取统计数据失败:', error)
  }
}

// 刷新数据
const refreshData = () => {
  searchSuppliers()
  getStatistics()
}

// 处理表单成功提交
const handleFormSuccess = () => {
  searchSuppliers()
  getStatistics()
}

onMounted(() => {
  searchSuppliers()
  getStatistics()
})
</script>

<style scoped>
.supplier-container {
  /* 移除padding，因为已经在父容器中设置了 */
}

.supplier-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 30px;
}

.header-left h2 {
  margin: 0 0 10px 0;
  color: #2c3e50;
  font-size: 28px;
  font-weight: 600;
}

.statistics {
  display: flex;
  gap: 20px;
}

.stat-item {
  background: #f8f9fa;
  padding: 8px 12px;
  border-radius: 4px;
  font-size: 14px;
  color: #666;
  border: 1px solid #e9ecef;
}

.header-right {
  display: flex;
  gap: 10px;
  align-items: center;
}

.supplier-header h2 {
  margin: 0;
  color: #2c3e50;
  font-size: 28px;
  font-weight: 600;
}

.add-btn {
  background: #3498db;
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s ease;
}

.refresh-btn {
  background: #95a5a6;
  color: white;
  border: none;
  padding: 12px 20px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s ease;
}

.refresh-btn:hover {
  background: #7f8c8d;
}

.add-btn:hover {
  background: #2980b9;
}

.search-form {
  background: #f8f9fa;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
}

.search-row {
  display: flex;
  gap: 20px;
  margin-bottom: 15px;
}

.search-item {
  display: flex;
  flex-direction: column;
  flex: 1;
}

.search-item label {
  font-size: 14px;
  color: #333;
  margin-bottom: 5px;
  font-weight: 500;
}

.search-item input,
.search-item select {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}

.search-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

.search-btn,
.reset-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: background-color 0.3s ease;
}

.search-btn {
  background: #27ae60;
  color: white;
}

.search-btn:hover {
  background: #229954;
}

.reset-btn {
  background: #95a5a6;
  color: white;
}

.reset-btn:hover {
  background: #7f8c8d;
}

.table-container {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  margin-bottom: 20px;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
}

.data-table th,
.data-table td {
  padding: 12px;
  text-align: left;
  border-bottom: 1px solid #eee;
}

.data-table th {
  background: #f8f9fa;
  font-weight: 600;
  color: #333;
}

.data-table tr:hover {
  background: #f8f9fa;
}

.status-active {
  color: #27ae60;
  font-weight: 500;
}

.status-inactive {
  color: #e74c3c;
  font-weight: 500;
}

.address-cell {
  display: flex;
  align-items: center;
  gap: 5px;
}

.address-text {
  flex: 1;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.coordinate-badge {
  font-size: 12px;
  color: #1890ff;
}

.action-btn {
  padding: 4px 8px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  margin-right: 5px;
  transition: background-color 0.3s ease;
}

.view-btn {
  background: #3498db;
  color: white;
}

.view-btn:hover {
  background: #2980b9;
}

.edit-btn {
  background: #f39c12;
  color: white;
}

.edit-btn:hover {
  background: #e67e22;
}

.delete-btn {
  background: #e74c3c;
  color: white;
}

.delete-btn:hover {
  background: #c0392b;
}

.pagination {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 0;
}

.pagination-info {
  color: #666;
  font-size: 14px;
}

.pagination-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.page-btn {
  padding: 6px 12px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s ease;
}

.page-btn:hover:not(:disabled) {
  background: #f8f9fa;
  border-color: #3498db;
}

.page-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.page-info {
  font-size: 14px;
  color: #666;
  margin: 0 10px;
}

.page-size-select {
  padding: 6px 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
}
</style> 